/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "tricolor_light_demo.h"

void TricolorLightInit(void)
{
    IoTGpioInit(IOT_GPIO_IO_GPIO_10); // 红灯
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_10, IOT_IO_FUNC_GPIO_10_PWM1_OUT);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_10, IOT_GPIO_DIR_OUT);

    IoTGpioInit(IOT_GPIO_IO_GPIO_11); // 绿灯
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_11, IOT_IO_FUNC_GPIO_11_PWM2_OUT);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_11, IOT_GPIO_DIR_OUT);

    IoTGpioInit(IOT_GPIO_IO_GPIO_12); // 蓝灯
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_12, IOT_IO_FUNC_GPIO_12_PWM3_OUT);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_12, IOT_GPIO_DIR_OUT);

    IoTPwmInit(IOT_PWM_PORT_PWM1);
    IoTPwmInit(IOT_PWM_PORT_PWM2);
    IoTPwmInit(IOT_PWM_PORT_PWM3);
}

void TricolorLightStart(char redDuty, char GreenDuty, char BlueDuty)
{
    int ret = IOT_SUCCESS;
    
    ret |= IoTPwmStart(IOT_PWM_PORT_PWM1, redDuty,   40000);
    ret |= IoTPwmStart(IOT_PWM_PORT_PWM2, GreenDuty, 40000);
    ret |= IoTPwmStart(IOT_PWM_PORT_PWM3, BlueDuty,  40000);

    if(ret != IOT_SUCCESS)
    {
        printf("iot pwm start fail \r\n");
    }
}

void TricolorLightStop(void)
{ 
    int ret = IOT_SUCCESS;
    ret |= IoTPwmStop(IOT_PWM_PORT_PWM1);
    ret |= IoTPwmStop(IOT_PWM_PORT_PWM2);
    ret |= IoTPwmStop(IOT_PWM_PORT_PWM3);

    if(ret != IOT_SUCCESS)
    {
        printf("iot pwm stop fail \r\n");
    }
}